﻿function CheckBoxNewChecked(obj) {    
    var ddlCurrency = document.getElementById("ctl00_Body_ddlCorrectCurrency");
    if (!obj.checked)
        return;
        
    var chkChange = document.getElementById("ctl00_Body_chkModify");
    chkChange.checked = false;
    ddlCurrency.value = "NTD";

    var textbox = document.getElementById("ctl00_Body_txtCorrectAmount");
    textbox.value = 0;
}

function CheckBoxChangeChecked(obj) {
    if (!obj.checked)
        return;
    
    var ddlCurrency = document.getElementById("ctl00_Body_ddlCorrectCurrency");
    ddlCurrency.value = "";

    var textbox = document.getElementById("ctl00_Body_txtCorrectAmount");
    textbox.value = "";

    var chkNew = document.getElementById("ctl00_Body_chkNew");
    chkNew.checked = false;
}

function ShowChoiceDiv(strDivID,width,height) {
    debugger;
    //如果是选择资产编号，先检查是否有选资产类别
    if (strDivID == "divAsset")
        ChoiceAsset();
    if (strDivID == "divPR")
        ChoicePR();
        

    ShowBackgroundDiv();
    
    var div = document.getElementById(strDivID);
    div.style.left = (document.body.offsetWidth - width) / 2;
    div.style.top = (document.body.offsetHeight - height) / 2;
    div.style.display = "block";
    div.style.height = height;
    div.style.width = width;
}

function ChoiceAsset() {
    var assetType = document.getElementById("ctl00_Body_ddlAssetType").value;
    if (assetType == "") {
        alert("請先選擇資產類別！");
        return;
    }
    var txtTable = document.getElementById("ctl00_Body_UCSelectAssetNO1_txtQueryTableName");
    if (assetType == "1")
        txtTable.value = "tbl_master";
    else if (assetType == "2")
        txtTable.value = "tbl_control";
}

function ChoicePR() {
    var txtTable = document.getElementById("ctl00_Body_UCSelectPR1_txtTableName");
    txtTable.value = "resda";
}

function ShowBackgroundDiv() {
    var obj = document.getElementById("divBack");
    obj.style.display = "block";
    obj.style.width = document.body.clientWidth;
    obj.style.height = document.body.clientHeight;
}

function Unlock() {
    var backObj = document.getElementById("divBack");
    backObj.style.display = "none";

    var aryDiv = new Array("divUser", "divAsset", "divAttach", "divImages", "divPR");
    for (var i = 0; i < aryDiv.length; i++) {
        var obj = document.getElementById(aryDiv[i]);
        if (obj == null)
            continue;
        obj.style.display = "none";        
    }
}

function SetAssetSelectedValue(strAssetNO, strKeepDept, strKeeper, strChiName, strBrand, strSpec, strModel, strKeeperNO) {
    Unlock();   
    document.getElementById("ctl00_Body_txtAssetNO").value = strAssetNO;
    document.getElementById("ctl00_Body_lblKeepDept").innerHTML = strKeepDept;
    document.getElementById("ctl00_Body_lblKeeper").innerHTML = strKeeper;
    document.getElementById("ctl00_Body_lblChiName").innerHTML = strChiName;
    document.getElementById("ctl00_Body_lblBrand").innerHTML = strBrand;
    document.getElementById("ctl00_Body_lblSpec").innerHTML = strSpec;
    document.getElementById("ctl00_Body_lblModuleNO").innerHTML = strModel;
    document.getElementById("ctl00_Body_txtAssetNO").onchange();  
}

function SetApplicantValue(strEmpNO, strEmpChiName, strEngName, strDeptNO, strExtNO) {
    Unlock();
    document.getElementById("ctl00_Body_lblApplicant").innerHTML = strEmpChiName + " - " + strExtNO;
    document.getElementById("ctl00_Body_txtApplicantNO").value = strEmpNO;
    document.getElementById("ctl00_Body_lblApplyDept").innerHTML = strDeptNO;
}

function UploadAttach(strAttachURL,strAttachInfo) {
    //返回多个Attach的信息，多个Attach之间以；间隔，而一个Attach之间以“-”分隔客户端名字和服务器端名字
    Unlock();
    document.getElementById("ctl00_Body_lblCorrectAttach").innerHTML = strAttachURL;
    document.getElementById("ctl00_Body_txtAttachInfo").value = strAttachInfo;
}

function UploadImage(strImageURL, strImageInfo) {
    Unlock();
    document.getElementById("ctl00_Body_lblCorrectPictures").innerHTML = strImageURL;
    document.getElementById("ctl00_Body_txtImageInfo").value = strImageInfo;
}

function SetPRValue(strPRNO) {
    Unlock();
    document.getElementById("ctl00_Body_txtPRNO").value = strPRNO;
}

function CheckBeforeSend() {
    var chkChange = document.getElementById("ctl00_Body_chkModify");
    var chkNew = document.getElementById("ctl00_Body_chkNew");

    var strErr = "";
    if (!chkNew.checked && !chkChange.checked)
        strErr += "未選擇表單申請狀態！\n";
       
    var txtReason = document.getElementById("ctl00_Body_txtApplyReason");
    if (txtReason.value == "")
        strErr += "未填寫申請原因！\n";

    var ddlAssetType = document.getElementById("ctl00_Body_ddlAssetType");
    if (ddlAssetType.value == "")
        strErr += "未選擇資產類別！\n";

    var txtAssetNO = document.getElementById("ctl00_Body_txtAssetNO");
    if (txtAssetNO.value == "")
        strErr += "未輸入資產編號！\n";

    var txtPRNO = document.getElementById("ctl00_Body_txtPRNO");
    if (txtPRNO.value == "")
        strErr += "未輸入PR單號！\n";

    var txtBuyDate = document.getElementById("ctl00_Body_txtBuyDate");
    if (txtBuyDate.value == "")
        strErr += "未輸入購買日期！\n";

    var txtFirstCorrectDate = document.getElementById("ctl00_Body_txtFirstCorrectDate");
    if (txtFirstCorrectDate.value == "")
        strErr += "未輸入首校日期！\n";

    var txtNeedCorrectDatae = document.getElementById("ctl00_Body_txtNeedCorrectDate");
    if (txtNeedCorrectDatae.value == "")
        strErr += "為輸入應校日期！\n";

    var txtCorrectAmount = document.getElementById("ctl00_Body_txtCorrectAmount");
    if (txtCorrectAmount.value == "")
        strErr += "未輸入校驗費用！\n";

    var ddlCorrectCycle = document.getElementById("ctl00_Body_ddlCorrectCycle");
    if (ddlCorrectCycle.value == "")
        strErr += "未選擇校驗週期！\n";

    var ddlCurrentStatus = document.getElementById("ctl00_Body_ddlCurrentStatus")
    if (ddlCurrentStatus.value == "")
        strErr += "未选择目前状态！\n";

    var txtAttach = document.getElementById("ctl00_Body_txtAttachInfo");
    if (ddlCurrentStatus.value == "1" || ddlCurrentStatus.value == "4") {
        if (txtAttach.value == "")
            strErr += "當為需校驗或免驗時，需上傳校驗報告/免驗申請書！\n";
    }

    var ddlCorrectType = document.getElementById("ctl00_Body_ddlCorrectType");
    if (ddlCorrectType.value == "")
        strErr += "未選擇校驗區分！\n";

    if (strErr.length > 0) {
        alert(strErr);
        return false;
    }

    strErr = "";
    if (chkNew.checked) {        
        if (txtAttach.value == "")
            strErr += "表單狀態為new時，需上傳儀器附件！\n";

        var txtImage = document.getElementById("ctl00_Body_txtImageInfo");
        if (txtImage.value == "")
            strErr += "表單狀態為new時，需上傳儀器圖片！\n";

    }

    if (strErr.length > 0) {
        alert(strErr);
        return false;
    }

    return true;
}

function test() {

    alert("asset no onchange!");
}